<?php

class ThonXom_Model extends MyModel
{
    
    function __construct()
    {
        parent::__construct();
    }
    
    function getThonXom($maThonXom)
    {
        $str = "SELECT mathonxom, tenthonxom, maphuongxa 
                FROM thonxom 
                WHERE mathonxom = '{$maThonXom}'";
                
        $query = mysql_query($str);
        return $query;
    }
    
    function getAllThonXom()
    {
        $str = "SELECT thonxom.mathonxom, thonxom.tenthonxom, phuongxa.tenphuongxa
                FROM thonxom
                INNER JOIN phuongxa
                ON thonxom.maphuongxa=phuongxa.maphuongxa
                ORDER BY thonxom.mathonxom";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    function getAllPhuongXa()
    {
        $str = "SELECT maphuongxa, tenphuongxa 
                FROM phuongxa";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    function checkExist($action, $maThonXom, $tenThonXom)
    {
        switch($action)
        {
            case "add":
                $str = "SELECT * 
                        FROM thonxom 
                        WHERE mathonxom = '{$maThonXom}'";
                        
                $query = mysql_query($str);
                $result = mysql_num_rows($query) > 0 ? true : false;
                if($result == false)
                {
                    $str = "SELECT * 
                            FROM thonxom 
                            WHERE tenthonxom = '{$tenThonXom}'";
                            
                    $query = mysql_query($str);
                    return mysql_num_rows($query) > 0 ? true : false;            
                }
                else 
                    return true;
                break;
                
            case "edit":
                $str = "SELECT * 
                        FROM thonxom 
                        WHERE tenthonxom = '{$tenThonXom}'";
                        
                $query = mysql_query($str);
                return mysql_num_rows($query) > 0 ? true : false;
                break;
        }
    }
    
    function addThonXom($maThonXom, $tenThonXom, $maPhuongXa)
    {
        $str = "INSERT INTO thonxom 
                VALUES('{$maThonXom}', '{$tenThonXom}', '{$maPhuongXa}')";
                
        mysql_query($str);
    }
    
    function editThonXom($maThonXom, $tenThonXom, $maPhuongXa)
    {
        $str = "UPDATE thonxom 
                SET tenthonxom = '{$tenThonXom}', maphuongxa = '{$maPhuongXa}'  
                WHERE mathonxom = '{$maThonXom}'";
                
        mysql_query($str);
    }    
    
    function deleteThonXom($maThonXom)
    {
        $str = "DELETE FROM thonxom 
                WHERE mathonxom = '{$maThonXom}'";
                
        mysql_query($str);
    }
    
}

?>